package cn.org.timespace.easyframe.action.permission;

import java.io.PrintWriter;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.DictionaryConst;
import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.model.permission.EfUser;
import cn.org.timespace.easyframe.service.permission.EfUserService;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;
import cn.org.timespace.easyframe.util.web.Security;

public class EFUserAction extends EFBaseAction{

	private static final long serialVersionUID = -3416183313291298070L;

	private static Log log=LogFactory.getLog(EFUserAction.class);
	
	private EfUserService efUserService;
	
	public EfUserService getEfUserService() {
		return efUserService;
	}
	public void setEfUserService(EfUserService efUserService) {
		this.efUserService = efUserService;
	}

	public EFUserAction()
	{
		super(new EFUserModel());
	}
	
	EFUserModel efUserModel=(EFUserModel)this.getModel();
	
	public String doList() throws Exception{

		List<EfUser> efUserList = efUserService.findByCondition(efUserModel);
		getRequest().setAttribute(VOLIST, efUserList);
	
		return LIST;
	}
	
	public String doSave() throws Exception{
		EfUser efUser=efUserModel.getEfUser();
		efUser.setUserId(IdentityGenerator.getInstance().getId());
		//密码加密
		String password = efUser.getUserPassword();
		String encryptPW = Security.encrypt(password);
		efUser.setUserPassword(encryptPW);
		if(log.isInfoEnabled()) log.info("当前密码是："+password+"，加密之后是："+encryptPW);
		//设置默认的用户状态
		efUser.setDicUserIsValid(DictionaryConst.DicUserIsValid_VALID);
		
		efUserService.save(efUser);
		//保存用户扩展表
//		UserInfo userInfo  =new UserInfo();
//		userInfo.setUserName(efUser.getUserAccount());
//		userInfo.setCode(efUser.getCode());
//		userInfo.setCoderandom(efUser.getCoderandom());
//		userInfoService.save(userInfo);
		return doList();
	}

	public String doUpdate() throws Exception {
		EfUser efUser = efUserModel.getEfUser();
		efUserService.update(efUser);
//		UserInfo userInfo = userInfoService.findByUserAccount(efUser.getUserAccount());
//		userInfo.setCode(efUser.getCode());
//		userInfo.setCoderandom(efUser.getCoderandom());
//		userInfoService.update(userInfo);
		return doList();
	}

	public String doDelete() throws Exception{

		String userId = getRequest().getParameter("userId") ;
		EfUser efUser = efUserService.findById(userId);
		efUser.setDicUserIsValid("-1");//将该用户软删除
		efUserService.update(efUser);
		//efUserService.deleteUser(efUser);
		
		return doList();
	}

	public String doFindById() throws Exception{
		String userId = getRequest().getParameter("userId") ;
		EfUser efUser = new EfUser();
		if(!StringUtils.isEmpty(userId)){  //更新
			 efUser = efUserService.findById(userId);
			efUserModel.setEfUser(efUser);
//			userInfo = userInfoService.findByUserAccount(efUser.getUserAccount());
//			efUser.setCode(userInfo.getCode());
//			efUser.setCoderandom(userInfo.getCoderandom());
			return UPDATE;
		}
		//厂家随机码
//		String code = IdentityGenerator.getInstance().getCharacterAndNumber(6);
//		efUser.setCoderandom(code);
		efUserModel.setEfUser(efUser);
		return ADD;
		
	}
	
	/**
	 * 校验用户账号是否存在
	 * @return 存在返回 true 不存在返回false
	 * @throws Exception
	 */
	public String doCheckUserAccountIsExist() throws Exception{
		String result = "true"; //存在
		
		String userAccount = efUserModel.getUserAccount();
		EfUser efUser = efUserService.findByUserAccount(userAccount);
		if(efUser == null){
			result = "false"; //不存在
		}
		
		getResponse().setCharacterEncoding("UTF-8");
		PrintWriter out = getResponse().getWriter();
		out.write(result);
		
		return null;
	}
	
	/**
	 * 修改用户密码之前
	 * @return
	 * @throws Exception
	 */
	public String doUpdatePWBefore() throws Exception {
		efUserModel.setUserId(getUserId());
		
		return "userPWUpdate";
	}
	
	/**
	 * 修改用户密码
	 * @return
	 * @throws Exception
	 */
	public String doUpdatePW() throws Exception {
		String pageMsg = "修改密码成功，请记住您设置的请密码！";
		
		String newPw = Security.encrypt(efUserModel.getUserPasswordNew());
		String oldPw = Security.encrypt(efUserModel.getUserPasswordOld());
		String userId = efUserModel.getUserId();
		
		//查询用户旧密码
		EfUser efUser = efUserService.findById(userId);
		String sysOldPw = efUser.getUserPassword();
		
		if(!oldPw.equals(sysOldPw)){
			pageMsg = "旧密码错误，请重新输入！";
			getRequest().setAttribute(PAGEMSG, pageMsg);
			return "userPWUpdate";
		}
		
		try{
			efUserService.updatePW(userId, newPw);
		}
		catch(Exception e){
			pageMsg = "修改密码失败！原因是：<"+e.getMessage()+">";
		}
		
		getRequest().setAttribute(PAGEMSG, pageMsg);
		return "userPWUpdate";
	}	

	
}
